using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Persistence;
using Sedna.Util;
using Spring.Util;

namespace Sedna.Core.Controls.Actions
{
    public class TranslateToCollectionProviderAction: AbstractAction
    {
        #region Fields
        
        private string collectionPath;
        private string providerPath;
        private string sortExpression;
        private bool isAsc;

        #endregion

        #region Properties
        
        public string CollectionPath
        {
            set { collectionPath = value; }
        }

        public string ProviderPath
        {
            set { providerPath = value; }
        }

        public string SortExpression
        {
            set { sortExpression = value; }
        }

        public bool IsAsc
        {
            set { isAsc = value; }
        }

        #endregion

        #region Execute
        
        public override IActionResult Execute(IExecutionContext executionContext)
        {
            if ((ICollection)scopePathParser.Parse(collectionPath).Value != null)
            {
                scopePathParser.Parse(providerPath).Value = new CollectionProvider((ICollection)scopePathParser.Parse(collectionPath).Value);
                if (!StringUtils.IsNullOrEmpty(sortExpression))
                {
                    ((IDataProvider)scopePathParser.Parse(providerPath).Value).SortExpression = sortExpression;
                    ((IDataProvider)scopePathParser.Parse(providerPath).Value).IsAsc = isAsc;
                }
            }
            return CreateResult();
        }

        #endregion
    }
}
